package com.x.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import javax.xml.crypto.Data;

/**
 * ClassName: MapperJavaConfig
 * Package: IntelliJ IDEA
 * Description:持久化层配置类
 *      1、
 *      2、
 *      3、
 *      4、
 * Written by HanXu
 * Create 2024.09.15 15:23
 */

@Configuration
public class MapperJavaConfig {


    //配置SqlSessionFactoryBean，MyBatis通过工厂类创建SqlSessionFactory
    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();

        sqlSessionFactoryBean.setDataSource(dataSource);

        Resource resource = new ClassPathResource("mybatis-config.xml");

        sqlSessionFactoryBean.setConfigLocation(resource);

        return sqlSessionFactoryBean;
    }


    //添加MapperScannerConfigurer，扫描mapper包
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("com.x.mapper");
        return mapperScannerConfigurer;
    }
}
